﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IFTRUE.FMS.Web
{
    /// <summary>
    /// 作用：一般处理程序安全检查
    /// 作者：iftrue
    /// 日期：2013-08-21
    /// </summary>
    public class AshxCheck
    {
        /// <summary>
        /// 安全检查
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public static bool Check(System.Web.HttpContext context,ref string msg)
        {
            //首先判断请求是否来自同一个域
            try
            {
                if (context.Request.ServerVariables["HTTP_Referer"] == null || context.Request.ServerVariables["REMOTE_HOST"] == null || context.Request.ServerVariables["SERVER_NAME"] != context.Request.Url.Host)
                {
                    Model.Log logModel = new Model.Log();
                    logModel.Content = "跨域请求(" + context.Request.Url.ToString() + ")";
                    logModel.CreateDate = DateTime.Now;
                    logModel.IP = Helper.IpHelper.GetClientIP();
                    logModel.Title = "域名检查";
                    logModel.Type = 3;
                    new BLL.Log().Add(logModel);

                    msg="不可以跨域访问（" + context.Request.Url.Host + "）";
                    return false;
                }
            }
            catch (Exception ex)
            {
                Model.Log logModel = new Model.Log();
                logModel.Content = "域名检查异常(" + context.Request.Url.ToString() + ")(" + ex.Message + ")";
                logModel.CreateDate = DateTime.Now;
                logModel.IP = Helper.IpHelper.GetClientIP();
                logModel.Title = "域名检查";
                logModel.Type = 4;
                new BLL.Log().Add(logModel);
                msg = "系统异常，请联系管理员";
                return false;
            }
            //判断用户是否登录
            if (MySession.CurrentUser == null)
            {
                msg = "请先登录系统";
                return false;
            }
            return true;
        }
    }
}